import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
    meta: {
      isPublic: true
    }
  },
  {
    path: '/',
    name: 'main',
    component: () => import('../views/Main.vue'),
    children: [
      {
        path: '/categories/create',
        name:'categorycreate',
        component: () => import('../views/Category/CategoryEdit.vue')
      },
      {
        path: '/categories/edit/:id',
        name:'categoryedit',
        props: true,
        component: () => import('../views/Category/CategoryEdit.vue')
      },
      {
        path: '/categories/list',
        name:'categorylist',
        component: () => import('../views/Category/CategoryList.vue')
      },
      {
        path: '/items/create',
        name:'itemcreate',
        component: () => import('../views/Item/ItemEdit.vue')
      },
      {
        path: '/items/edit/:id',
        name:'itemedit',
        props: true,
        component: () => import('../views/Item/ItemEdit.vue')
      },
      {
        path: '/items/list',
        name:'itemlist',
        component: () => import('../views/Item/ItemList.vue')
      },
      {
        path: '/heroes/create',
        name: 'herocreate',
        component: () => import('../views/Hero/HeroEdit.vue')
      },
      {
        path: '/heroes/edit/:id',
        name: 'heroedit',
        props: true,
        component: () => import('../views/Hero/HeroEdit.vue')
      },
      {
        path: '/heroes/list',
        name: 'herolist',
        component: () => import('../views/Hero/HeroList.vue')
      },
      {
        path: '/articles/create',
        name:'articlecreate',
        component: () => import('../views/Article/ArticleEdit.vue')
      },
      {
        path: '/articles/edit/:id',
        name: 'articleedit',
        props: true,
        component: () => import('../views/Article/ArticleEdit.vue')
      },
      {
        path: '/articles/list',
        name: 'articlelist',
        component: () => import('../views/Article/ArticleList.vue')
      },
      {
        path: '/advert/create',
        name:'advertcreate',
        component: () => import('../views/Advert/AdvertEdit.vue')
      },
      {
        path: '/advert/edit/:id',
        name: 'advertedit',
        props: true,
        component: () => import('../views/Advert/AdvertEdit.vue')
      },
      {
        path: '/advert/list',
        name: 'advertlist',
        component: () => import('../views/Advert/AdvertList.vue')
      },
      {
        path: '/admin_user/create',
        name:'admin_usercreate',
        component: () => import('../views/AdminUser/AdminUserEdit.vue')
      },
      {
        path: '/admin_user/edit/:id',
        name: 'admin_useredit',
        props: true,
        component: () => import('../views/AdminUser/AdminUserEdit.vue')
      },
      {
        path: '/admin_user/list',
        name: 'admin_userlist',
        component: () => import('../views/AdminUser/AdminUserList.vue')
      },
    ]
}]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.admin_token) {
    return next('/login')
  }
  next()
})

export default router